package baidu.reader.common.model
{
    import baidu.lib.utils.*;
    import baidu.reader.common.model.VO.*;

    import flash.display.*;


    public class AppConfigModel extends Object
    {
        private var _loaderInfo:flash.display.LoaderInfo;

        private var _serialid:String;

        private var _content_url_template:String="http://wenku.baidu.com/as/play/${0}?${1}";

        private var _toc_url_template:String="http://wenku.baidu.com/sdir/${0}?lf=0";

        private var _default_rn:int=50;

        private var isDebug:Boolean=false;

        private var dirIndexXml:XML;

        private var _goLatestChpater:Boolean=false;

        private var _serialTotalPage:int;

        private var cachedPageArray:Array;

        public function AppConfigModel()
        {
            this.cachedPageArray = [];
            super();
            return;
        }

        private function parseLoaderInfo(arg1:flash.display.LoaderInfo):void
        {
            ////flashvars="docurl=http%3A%2F%2Fwenku.baidu.com%2Fas%2Fplay
            ///&docdirurl=http%3A%2F%2Fwenku.baidu.com%2Fsdir
            ///&serialid=e53eb4df42f38dfcf1e934c8
            ///&fpn=50&npn=50&ext=&readertype=internal
            ///&page=&offset=&docid=&debug=0
            ///&dir_index=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%3E%3Cpage%3E%3Ctotalpage%3E1090%3C%2Ftotalpage%3E%3Cdocindex%3E%3Cid%20key%3D%22845df7321d0619f661e990d8%22%3E0%3C%2Fid%3E%3Cid%20key%3D%22587e1418d03b8241ae17abd8%22%3E252%3C%2Fid%3E%3Cid%20key%3D%22d0c21d4f04dae17fa5cfa1d8%22%3E508%3C%2Fid%3E%3Cid%20key%3D%22901e51f143d0cf469ac4b1b0%22%3E776%3C%2Fid%3E%3Cid%20key%3D%226fa176d3cf469ac44fb3ceb0%22%3E1067%3C%2Fid%3E%3C%2Fdocindex%3E%3C%2Fpage%3E&lastChapter=0" allowfullscreen="true" wmode="opaque" bgcolor="#FFFFFF"
            //var loc1:*=arg1.parameters;

            //docurl=http://wenku.baidu.com/as/play&docdirurl=http://wenku.baidu.com/sdir&serialid=e53eb4df42f38dfcf1e934c8
            //&fpn=50&npn=50&ext=&readertype=internal&page=&offset=&docid=&debug=0&dir_index=<?xml version="1.0" encoding="utf-8"?><page><totalpage>1090</totalpage><docindex><id key="845df7321d0619f661e990d8">0</id><id key="587e1418d03b8241ae17abd8">252</id><id key="d0c21d4f04dae17fa5cfa1d8">508</id><id key="901e51f143d0cf469ac4b1b0">776</id><id key="6fa176d3cf469ac44fb3ceb0">1067</id></docindex></page>&lastChapter=0

            var loc1:Array = new Array();
            loc1["serialid"] = "e53eb4df42f38dfcf1e934c8";
            loc1["docurl"] = "http://wenku.baidu.com/as/play";
            loc1["docdirurl"]="http://wenku.baidu.com/sdir";
            loc1["fpn"] ="50";
            loc1["docid"] ="";
            loc1["debug"] ="0";
            loc1["offset"] ="";
            loc1["lastChapter"] = "0";
            loc1["readertype"] ="internal";
            loc1["dir_index"]= "<?xml version=\"1.0\" encoding=\"utf-8\"?><page><totalpage>1090</totalpage><docindex><id key=\"845df7321d0619f661e990d8\">0</id><id key=\"587e1418d03b8241ae17abd8\">252</id><id key=\"d0c21d4f04dae17fa5cfa1d8\">508</id><id key=\"901e51f143d0cf469ac4b1b0\">776</id><id key=\"6fa176d3cf469ac44fb3ceb0\">1067</id></docindex></page>";
            if (!loc1)
            {
                return;
            }
            this._serialid = loc1["serialid"];
            this._content_url_template = loc1["docurl"] + "/${0}?${1}";
            this._toc_url_template = loc1["docdirurl"] + "/${0}?lf=0";
            this._default_rn = int(loc1["fpn"]);
            this.isDebug = int(loc1["debug"]) != 1 ? false : true;
            this._goLatestChpater = int(loc1["lastChapter"]) != 1 ? false : true;
            if (this.isDebug)
            {
                this._content_url_template = this._content_url_template + "&debug=1";
                this._toc_url_template = this._toc_url_template + "&debug=1";
            }
            this.dirIndexXml = new XML(loc1["dir_index"]);
            this.parseDirXML();
        }

        private function parseDirXML():void
        {
            this._serialTotalPage = int(this.dirIndexXml.totalpage);
            return;
        }

        public function getFirstPage(arg1:String):int
        {
            var docid:String;

            var loc1:*;
            docid = arg1;
            if (!this.cachedPageArray[docid])
            {
                var loc3:*=0;
                var loc4:*=this.dirIndexXml.docindex.id;
                var loc2:*=new XMLList("");
                for each (var loc5:* in loc4)
                {
                    var loc6:*;
                    with (loc6 = loc5)
                    {
                        if (@key == docid)
                        {
                            loc2[loc3] = loc5;
                        }
                    }
                }
                this.cachedPageArray[docid] = int(loc2);
            }
            return this.cachedPageArray[docid];
        }

        public function getPositionBySerialPage(arg1:int):baidu.reader.common.model.VO.TocItemVO
        {
            var loc2:*=null;
            var loc3:*=0;
            var loc4:*=0;
            var loc1:*=[];
            var loc5:*=0;
            var loc6:*=this.dirIndexXml.docindex.id;
            for each (loc2 in loc6)
            {
                loc1[(int(loc2) + 1)] = String(loc2.@key);
            }
            loc3 = 0;
            loc4 = arg1;
            while (loc4 >= 0)
            {
                if (loc1[loc4])
                {
                    loc3 = arg1 - loc4 + 1;
                    break;
                }
                loc4 = (loc4 - 1);
            }
            return new baidu.reader.common.model.VO.TocItemVO(loc3, 0, "", loc1[loc4]);
        }

        public function getSerialPageByDocidPage(arg1:String, arg2:int):int
        {
            var docid:String;
            var item:XMLList;
            var pn:int;
            var result:int;

            var loc1:*;
            item = null;
            result = 0;
            docid = arg1;
            pn = arg2;
            var loc3:*=0;
            var loc4:*=this.dirIndexXml.docindex.id;
            var loc2:*=new XMLList("");
            for each (var loc5:* in loc4)
            {
                var loc6:*;
                with (loc6 = loc5)
                {
                    if (@key == docid)
                    {
                        loc2[loc3] = loc5;
                    }
                }
            }
            item = loc2;
            result = parseInt(item.toString()) + pn;
            return result;
        }

        public function getContentURL(arg1:String, arg2:String):String
        {
            return baidu.lib.utils.StringTemplate.output(this._content_url_template, arg1, arg2);
        }

        public function getTocURL():String
        {
            return baidu.lib.utils.StringTemplate.output(this._toc_url_template, this._serialid);
        }

        public function get content_url_template():String
        {
            return this._content_url_template;
        }

        public function set content_url_template(arg1:String):void
        {
            this._content_url_template = arg1;
            return;
        }

        public function get toc_url_template():String
        {
            return this._toc_url_template;
        }

        public function set toc_url_template(arg1:String):void
        {
            this._toc_url_template = arg1;
            return;
        }

        public function get loaderInfo():flash.display.LoaderInfo
        {
            return this._loaderInfo;
        }

        public function set loaderInfo(arg1:flash.display.LoaderInfo):void
        {
            this._loaderInfo = arg1;
            this.parseLoaderInfo(this._loaderInfo);
            return;
        }

        public function get serialid():String
        {
            return this._serialid;
        }

        public function set serialid(arg1:String):void
        {
            this._serialid = arg1;
            return;
        }

        public function get default_rn():int
        {
            return this._default_rn;
        }

        public function set default_rn(arg1:int):void
        {
            this._default_rn = arg1;
            return;
        }

        public function get serialTotalPage():int
        {
            return this._serialTotalPage;
        }

        public function get goLatestChpater():Boolean
        {
            return this._goLatestChpater;
        }


    }
}
